import util from '../../utils/util.js';
import { getCity } from '../../api/api.js';
import {
  userEdit,getUserInfo
} from "../../api/user.js"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': '电子名片',
    },
    putData:{
    avatar:"",
    nickname:"",
    sex:"",
    height:"",
    weight:"",
    edu:"",
    native_place:"",
    nation:"",
    school:"",
    position:'',
    self_desc:"",
    sport:"",
    },
    region: ['省', '市', '区'],
    multiArray: [0, 0, 0],
    multiArray:[],
    multiIndex: [0, 0, 0],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCityList()
    this.getUserInfo()
  },
  getUserInfo(){
    let that = this
    getUserInfo().then(res=>{
      let resData = res.data
      that.setData({
        putData:{
          avatar:resData.avatar,
          nickname:resData.nickname,
          sex:resData.sex,
          height:resData.height,
          weight:resData.weight,
          edu:resData.edu,
          native_place:resData.native_place,
          nation:resData.nation,
          school:resData.school,
          position:resData.position,
          self_desc:resData.self_desc,
          sport:resData.sport,
        },
        region:[resData.province||'省',resData.city||'市',resData.area||'区']
      })
    })
  },
  getCityList:function(){
    let that = this;
    getCity().then(res=>{
      that.setData({ district:res.data});
      that.initialize();
    })
  },
  initialize:function(){
    let that = this, province = [], city = [], area = [];
    if (that.data.district.length) {
      let cityChildren = that.data.district[0].c || [];
      let areaChildren = cityChildren.length ? (cityChildren[0].c || []) : [];
      that.data.district.forEach(function (item) {
        province.push(item.n);
      });
      cityChildren.forEach(function (item) {
        city.push(item.n);
      });
      areaChildren.forEach(function (item) {
        area.push(item.n);
      });
      that.setData({
        multiArray: [province, city, area],
      });
    }
  },
  bindRegionChange: function (e) {
    let multiIndex = this.data.multiIndex, province = this.data.district[multiIndex[0]] || { c: [] }, city = province.c[multiIndex[1]] || { v: 0 }, multiArray = this.data.multiArray, value = e.detail.value;
    console.log(value);
    console.log(province);
    this.setData({
      region: [multiArray[0][value[0]], multiArray[1][value[1]], multiArray[2][value[2]]],
      cityId: city.v,
      valueRegion: [0,0,0]
    });
    this.initialize();
  },
  bindMultiPickerColumnChange:function(e){
    let that = this, column = e.detail.column, value = e.detail.value, currentCity = this.data.district[value] || { c: [] }, multiArray = that.data.multiArray, multiIndex = that.data.multiIndex;
    multiIndex[column] = value;
    switch (column){
      case 0:
        let areaList = currentCity.c[0] || { c: [] };
        multiArray[1] = currentCity.c.map((item)=>{
          return item.n;
        });
        multiArray[2] = areaList.c.map((item)=>{
          return item.n;
        });
      break;
      case 1:
        let cityList = that.data.district[multiIndex[0]].c[multiIndex[1]].c || [];
        multiArray[2] = cityList.map((item)=>{
          return item.n;
        });
      break;
      case 2:
      
      break;
    }
    this.setData({ multiArray: multiArray, multiIndex: multiIndex});
  },
  /**
   * 选择性别
   * 
   */
  selectSex(e){
    this.setData({'putData.sex':e.detail.value})
  },
/**
   * 上传文件
   * 
   */
  uploadpic: function () {
    var that = this;
    util.uploadImageOne('upload/image', function (res) {
      that.setData({'putData.avatar':res.data.url})
    });
  },
  checkData(e){
    var that = this,
    value = e.detail.value;
    if (!value.nickname) return app.Tips({
      title: '请填写昵称'
    });
    if (that.data.region[0]=='省') return app.Tips({
      title: '请选择所在地'
    });
    let putData = value
    putData.avatar = that.data.putData.avatar
    putData.province =that.data.region[0]
    putData.city =that.data.region[1]
    putData.area =that.data.region[2]
    putData.sex = that.data.putData.sex
    that.setData({
      putData:putData
    })
    wx.showModal({
     title:'提示',
     content:'请确认信息是否填写正确！',
     success(){
     that.putData()
     }
    })
  },
  putData(){
    wx.showLoading({
      title: '提交中...',
    })
    userEdit(this.data.putData).then(res=>{
      wx.hideLoading()
      app.Tips({
        title: res.msg
      },{tab:3});
    }).catch(err=>{
      wx.hideLoading()
      wx.showModal({
        title:'提交失败',
        content:err
       })
    })
  }
  
})